﻿using DTM.Core.Exceptions;
using DTM.Domain.Players;
using DTM.Model;
using FluentValidation.Results;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NSubstitute;

namespace DTM.WinApp.Players.Tests.Units
{
    [TestClass()]
    public class PlayerPresenterTests
    {
        private PlayerPresenter _presenter;
        private IApplicationManager _manager;
        private IPlayerView _view;
        private IPlayerService _service;

        public IApplicationManager Substitue { get; private set; }

        [TestInitialize]
        public void Initialize()
        {
            _manager = Substitute.For<IApplicationManager>();
            _view = Substitute.For<IPlayerView>();
            _service = Substitute.For<IPlayerService>();
            _presenter = new PlayerPresenter(_manager, _service);
            _presenter.Initialize(_view);
        }

        [TestMethod()]
        public void SavePlayer_InputCorrect_PlayerInserted()
        {
            _view.SavePlayer += Raise.Event();

            _service.ReceivedWithAnyArgs(1).Insert(null);
        }

        [TestMethod()]
        public void SavePlayer_InputIncorrect_ErrorMessageIsShown()
        {
            _service
                .WhenForAnyArgs(x => x.Insert(default(IPlayer)))
                .Do(x =>
                {
                    throw new CustomValidationException(new ValidationFailure[] { new ValidationFailure("name", "error") });
                });

            _view.SavePlayer += Raise.Event();

            _manager.ReceivedWithAnyArgs(1).ShowErrorMessage("");
        }

        [TestMethod()]
        public void UpdatePlayer_InputCorrect_PlayerInserted()
        {
            _view.UpdatePlayer += Raise.Event();

            _service.ReceivedWithAnyArgs(1).Update(null);
        }

        [TestMethod()]
        public void UpdatePlayer_InputIncorrect_ErrorMessageIsShown()
        {
            _service
                .WhenForAnyArgs(x => x.Update(default(IPlayer)))
                .Do(x =>
                {
                    throw new CustomValidationException(new ValidationFailure[] { new ValidationFailure("name", "error") });
                });

            _view.UpdatePlayer += Raise.Event();

            _manager.ReceivedWithAnyArgs(1).ShowErrorMessage("");
        }
    }
}